/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.eventcase.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.course.vo.CourseVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.fanrenqiji.modules.business.eventcase.vo.EventCaseVO;
import com.fanrenqiji.modules.business.eventcase.service.EventCaseService;

@Controller
@RequestMapping(value = "${adminPath}/business/eventcase")
public class EventCaseController extends BaseController {
	
    private static Logger log = LoggerFactory.getLogger(EventCaseController.class);
    @Autowired
	private EventCaseService eventCaseService;

@ModelAttribute("eventCase")
public EventCaseVO getEventCase(@RequestParam(required = false) String id){
	if(StringUtils.isNotEmpty(id)){
		return eventCaseService.get(id);
	}else{
		return new EventCaseVO();
	}
}

/**
 * 列表
 *
 * @return
 */
@RequiresPermissions("business:eventcase:view")
@RequestMapping(value = {"list", ""})
public String list(EventCaseVO eventCase,HttpServletRequest request,
		HttpServletResponse response,Model model){
	Page<EventCaseVO> page = eventCaseService.findEventCaseByPage(new Page<EventCaseVO>(request,response),
		eventCase);
	model.addAttribute("page",page);
	model.addAttribute("eventCase",eventCase);
	return"modules/business/eventcase/eventcaseList";
}

/**
 * 选择列表
 *
 * @return
 */
@RequestMapping(value = "select")
public String select(EventCaseVO eventCase,HttpServletRequest request,HttpServletResponse response,Model model){
	Page<EventCaseVO> page = eventCaseService.findEventCaseByPage(new Page<EventCaseVO>(request,response,10),eventCase);
	model.addAttribute("page",page);
	model.addAttribute("eventCase",eventCase);
	return"modules/business/eventcase/eventcaseSelect";
}

/**
 * 用户添加更新表
 *
 * @param model
 * @return
 */
@RequiresPermissions("business:eventcase:view")
@RequestMapping(value = "form")
public String form(Model model,EventCaseVO eventCase){
	if (StringUtils.isNotEmpty(eventCase.getId())) {
		model.addAttribute("eventCase", eventCaseService.get(eventCase.getId()));
	}else{
		model.addAttribute("eventCase",eventCase);
	}
	return"modules/business/eventcase/eventcaseForm";
}

/**
 * 添加或修改提交
 *
 * @param eventCase
 * @param model
 * @param redirectAttributes
 * @return
 */
@RequiresPermissions("business:eventcase:edit")
@RequestMapping(value = "save")
public String save(EventCaseVO eventCase,Model model,
		RedirectAttributes redirectAttributes){// TODO @Valid 添加后端校验
	if(!beanValidator(model,eventCase)){
		return form(model,eventCase);
	}
	eventCaseService.update(eventCase);
	addMessage(redirectAttributes,"保存成功");
	return"redirect:"+GlobalSetting.getAdminPath()
		+"/business/eventcase/?repage";
}

/**
 * 删除操作
 *
 * @return
 */
@RequestMapping("delete")
@RequiresPermissions("business:eventcase:edit")
public String delete(String id, RedirectAttributes redirectAttributes) {
	eventCaseService.delete(id);
	addMessage(redirectAttributes, "删除成功");
	return "redirect:" + GlobalSetting.getAdminPath() + "/business/eventcase/";
}

	/**
	 * 将上传的事件进行保存(其实就是作业)
	 * @param courseid
	 * @param eventid
	 * @return
	 */
	@RequestMapping(value = "saveevent")
@RequiresPermissions(value={"business:course:addtask"})
public String saveevent(String courseid,String eventid){
		eventCaseService.saveevent(courseid,eventid);
return  "redirect:" + GlobalSetting.getAdminPath() + "/business/course/addeventcase?id="+courseid;
}

	/**
	 * 删除事件实例的方法(作业)
	 * @param courseid
	 * @param eventcaseid
	 * @return
	 */
@RequestMapping(value = "removeeventcase")
@RequiresPermissions("business:course:addtask")
public String removeeventcase(String courseid ,String eventcaseid){
eventCaseService.removeeventcase(courseid, eventcaseid);
return  "redirect:" + GlobalSetting.getAdminPath() + "/business/course/addeventcase?id="+courseid;
}
	/**
	 * 删除事件实例的方法（事件实例）
	 * @param courseid
	 * @param eventcaseid
	 * @return
	 */
	@RequestMapping(value = "removeeventcase3")
	@RequiresPermissions(value = {"business:course:addevent"})
	public String removeeventcase3(String courseid ,String eventcaseid){
		eventCaseService.removeeventcase(courseid, eventcaseid);
		return  "redirect:" + GlobalSetting.getAdminPath() + "/business/course/addevent?id="+courseid;
	}
	/**
	 * 添加问题的方法
	 * @param courseid
	 * @param eventcaseid
	 * @param questionid
	 * @return
	 */
@RequestMapping(value = "addquestion")
@RequiresPermissions(value={"business:course:addtask"})
public  String  addquestion(String courseid,String eventcaseid,String questionid){
		eventCaseService.addquestion(courseid, eventcaseid, questionid);
	return   "redirect:" + GlobalSetting.getAdminPath() + "/business/course/addeventcase?id="+courseid;
}

	/**
	 * 修改事件的时间
	 * @param id
	 * @param shijian
	 */
	@RequestMapping(value = "updatacourseitemevent")
@RequiresPermissions("business:course:beike")
	@ResponseBody
public String updatacourseitemevent(String id,String shijian){
	eventCaseService.updatacourseitemevent(id, shijian);
		return "1";
	}
	/**
	 * 将上传的事件进行保存(其实就是作业)
	 * @param courseid
	 * @param eventid
	 * @return
	 */
	@RequestMapping(value = "saveevent2")
	@RequiresPermissions("business:course:addtask")
	public String saveevent2(String courseid,String eventid){
		eventCaseService.saveevent(courseid,eventid);
		return  "redirect:" + GlobalSetting.getAdminPath() + "/business/course/addevent?id="+courseid;
	}
	/**
	 * 课程里面添加问题的方法
	 * @param courseid
	 * @param eventcaseid
	 * @param questionid
	 * @return
	 */
	@RequestMapping(value = "addquestion2")
	@RequiresPermissions("business:course:addevent")
	public  String  addquestion2(String courseid,String eventcaseid,String questionid) {
		eventCaseService.addquestion(courseid, eventcaseid, questionid);
		return "redirect:" + GlobalSetting.getAdminPath() + "/business/course/addevent?id=" + courseid;
	}

	/**
	 * 课程组里面添加问题的方法
	 * @param coursegroupid
	 * @param eventcaseid
	 * @param questionid
	 * @return
	 */
	@RequestMapping(value = "addquestion3")
	@RequiresPermissions("business:coursegroup:addeventcase")
	public  String  addquestion3(String coursegroupid,String eventcaseid,String questionid) {
		eventCaseService.addquestion(coursegroupid, eventcaseid, questionid);
		return "redirect:" + GlobalSetting.getAdminPath() + "/business/coursegroup/addeventcase?id=" + coursegroupid;
	}

	/**
	 * 课程组里面移除事件实例的方法
	 * @param coursegroupid
	 * @param eventcaseid
	 * @return
	 */
	@RequestMapping(value = "removeeventcase2")
	@RequiresPermissions(value={"business:coursegroup:addeventcase"})
	public String removeeventcase2(String coursegroupid,String eventcaseid){
		eventCaseService.removeeventcase2(coursegroupid, eventcaseid);
		// 重定向到课程组里面添加事件实例的方法
		return "redirect:" + GlobalSetting.getAdminPath() + "/business/coursegroup/addeventcase?id=" + coursegroupid;
	}

	/**
	 * k课程组里面添加事件实例的方法
	 * @param coursegroupid
	 * @param eventid
	 * @return
	 */
	@RequestMapping(value = "caoursegroup")
	@RequiresPermissions("business:coursegroup:addeventcase")
	public String caoursegroup(String coursegroupid,String eventid){
		eventCaseService.coursegroupandevent(coursegroupid, eventid);
		return "redirect:" + GlobalSetting.getAdminPath() + "/business/coursegroup/addeventcase?id=" + coursegroupid;
	}


	/**
	 * 根据事件实例中间表查询事件实例
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "selectByCourseItemEventId")
	@ResponseBody
	public EventCaseVO selectByCourseItemEventId(String id){
		EventCaseVO eventCaseVO = eventCaseService.selectByCourseItemEventId(id);
		return  eventCaseVO;
	}
}